<template>
  <!-- 添加审核人弹出框 -->
  <el-dialog
    :visible.sync="Visible"
    width="30%"
    append-to-body
    title="设置上限积分"
    :before-close="close"
  >
    <el-form :model="dataForm" :rules="rules" ref="dataForm">
      <!--      <el-form-item>-->
      <!--        <el-radio label="-1">无上限</el-radio>-->
      <!--        <el-radio label="1">设置值</el-radio>-->
      <!--      </el-form-item>-->
      <el-form-item label prop="type">
        <el-radio-group v-model="dataForm.type">
          <el-radio :label="-1" border>无上限</el-radio>
          <el-radio :label="1" border>设置值</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="dataForm.type == 1" label="设置上限积分:" prop="maxCount">
        <el-input-number controls-position="right" :min="1" v-model="dataForm.maxCount" style="width: 200px"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="onsubmit()" :loading="loading">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  export default {
    data() {
      return {
        loading: false,
        Visible: false,
        dataForm: {
          id: '',
          type: -1,
          maxCount: ''
        },
        rules: {
          maxCount: [
            {required: true, message: '上限积分不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init(id) {
        this.dataForm = {
          id: '',
          type: -1,
          maxCount: ''
        }
        this.dataForm.id = id
        this.getMaxCount(id)
        this.Visible = true
        this.loading = false
      },
      close() {
        this.Visible = false
      },
      onsubmit() {
        this.$refs['dataForm'].validate(valid => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl('/integral-admin/integral/rule/updateMaxCount'),
              method: 'post',
              data: {
                maxCount: parseInt(this.dataForm.type) === 1 ? this.dataForm.maxCount : -1,
                markId: this.dataForm.id
              }
            }).then(res => {
              // eslint-disable-next-line eqeqeq
              if (res.code == 200) {
                this.$notify.success({title: '成功', message: '修改上限值成功！'})
                this.$emit('refresh')
              } else {
                this.$notify.error({title: '错误', message: '修改上限值失败！'})
              }
              this.Visible = false
            })
          }
        })
      },
      getMaxCount(id) {
        this.$http({
          url: this.$http.adornUrl('/integral-admin/integral/rule/getMaxCount'),
          method: 'post',
          data: {
            markId: id
          }
        }).then(res => {
          // eslint-disable-next-line no-unused-expressions,eqeqeq
          // this.dataForm.maxCount == res.data
          this.dataForm.type = res.data > -1 ? 1 : -1
          this.dataForm.maxCount = res.data > -1 ? res.data : ''
          // // eslint-disable-next-line eqeqeq
          // if (res.code == 200) {
          //   this.$notify.success({title: '成功', message: '修改上限值成功！'})
          //   this.$emit('refresh')
          // } else {
          //   this.$notify.error({title: '错误', message: '修改上限值失败！'})
          // }
          // this.Visible = false
        })
      }
    }
  }
</script>

<style scoped>
</style>
